<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AnimusMVC
 *
 * @author Animus Inc.
 */

define('DOMROOT',dirname(dirname(dirname(__FILE__)).'/'));
define('APICORE',DOMROOT.'/classes/APICore/');

require_once APICORE.'/ModelView.php';
require_once APICORE.'/AnimusSession.php';

class AnimusMVC {

    private $_MVCMap;
    
    private $_ModelView;
    
    private $_component;
    
    public function __construct(){
        $this->setMVCMap(); 
    }

    
    public function setMVCMap(){
        $xml = new SimpleXMLElement(file_get_contents(DOMROOT.'/config/mvc-mapping.xml'));
        $this -> _MVCMap = $xml ->children();
        $this -> _ModelView = new ModelView();
        return;
        
        
    }
    
    public function modelViewLookup(AnimusRequest $Request){
        $_comp_name = $Request -> getComponent();
        foreach($this -> _MVCMap -> component as $_comp){
           if(strcmp($_comp_name, (string) $_comp->name) == 0 ) {
            foreach($_comp -> view as $view){
                $_view_name = (string)$view[name];
                $_view_type = (string)$view[type];
                $_view_path = (string)$view[path];
             switch($_view_type){
                    case "default" : {  if($_view_path){
                                         require_once $_view_path;
                                         $this -> _ModelView -> setDefaultView(new $_view_name());
                                       }
                                     }
                       break;
                    case "success" : {  if($_view_path){
                                         require_once $_view_path;
                                         $this -> _ModelView -> setSuccessView(new $_view_name());
                                       }
                                     }
             }
            }
             if((string)$_comp -> model[path]) {    
                 $_model_path = (string)$_comp -> model[path];
                 require_once $_model_path;
             }
             if((string)$_comp -> model[name]) {
                 $_model_name = (string)$_comp -> model[name];
                 $this -> _ModelView -> setModel(new $_model_name());
             }
         }
        }
        return;
    }
    
    public function invokeInjectedComponent(AnimusRequest $Request, AnimusResponse $Response){
        require_once $Request ->getComponentPath();// Require the component that has to be loaded
        $component_name = $Request -> getComponent();
        $component = new $component_name(); // Initialize the component
        $component -> setModelView($this -> _ModelView); // Inject ModelView
        $Request -> setSession(new AnimusSession()); 
        $component -> setRequest($Request);
        $component -> setResponse($Response);
        $this -> _component = $component;
        return;
        
    }

    public function runComponent(){
        return $this -> _component -> execute();
    }
    
    public function getResponse(){
        return $this -> _component -> getResponse();
    } 
}
?>
